const axios = require('axios');
const iconv = require('iconv-lite');
const cheerio = require('cheerio');

const getWBTopSearch = async () => {
    const response1 = await axios.get('https://s.weibo.com/top/summary?cate=realtimehot', {
        responseType: 'arraybuffer', headers: {
            'Cookie': 'SUB=_2AkMSo-OXf8NxqwFRmfsVyWPrb4h2zQzEieKk_xJMJRMxHRl-yT9kqm0vtRB6OSPNeKxPf5ejJ9RI8V998RQtyITRg0UM; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WFn87gwzH0a9vQUcSb_H54l; _s_tentry=passport.weibo.com; Apache=8814897233824.78.1711238307122; SINAGLOBAL=8814897233824.78.1711238307122; ULV=1711238307152:1:1:1:8814897233824.78.1711238307122:; b-user-id=2c82c72f-ef55-c1ac-02d9-25374aaa6c3b',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
        },
    });
    let decodedStr = iconv.decode(new Buffer.from(response1.data), 'utf8');
    const $ = cheerio.load(decodedStr);
    const table = $('.m-main').find('table tr');

    let WBData = [];
    table.each((i, row) => {
        const indexes = $(row).find('td').eq(0).text(); // 索引
        if (i > 1 && indexes !== '•') {
            const text = $(row).find('td').eq(1).find('a').text(); // 内容
            const href = `https://s.weibo.com/${$(row).find('td').eq(1).find('a').attr('href')}`; // 内容
            const hot = $(row).find('td').eq(1).find('span').text(); // 内容
            const type = $(row).find('td').eq(2).text(); // 内容
            WBData.push({
                indexes,
                text,
                href,
                hot,
                type,
            })
        }

    });
    return WBData;
}

const getZHTopSearch = async () => {
    const response = await axios.get('https://www.zhihu.com/hot', {
        responseType: 'arraybuffer',
        headers: {
            'Cookie': '_zap=fa0d5d66-3247-411e-a2c2-a71b9c1ca9a4; d_c0=AHAWHnKMWxiPTnr8Mb0z6fzmqSY5RdzZK_M=|1711240584; b-user-id=1c2e6385-cd29-de40-f0be-f50b63143535; __snaker__id=JRi4OvQtlHql419z; q_c1=4e110dc8d9674742af6d4ca1f38ea6d8|1711288161000|1711288161000; _xsrf=Zo4ovDGooR5aMH8OwsKkRoKGSCMUg2xz; __zse_ck=001_1hvkDOczvdO2SshZwNx+qblwAL+=7NC1Dd9GvtzuWlf9919dawJV7m7EDxN9AyJipNqieqUFYWugfMhhd4H2ztW+SaGRJwI+gzzEzGDDoFNsB2gpBo9iq5sePz/phhEZ; z_c0=2|1:0|10:1724519761|4:z_c0|80:MS4xN2g5NkNBQUFBQUFtQUFBQVlBSlZUY2F5b0dkS0I0UXZQWTVZWTRKeEJuVklPd3VoWFQtbGN3PT0=|eacc3efe07c9affdc9339bef131c6d74f574a33614c696cb93f7395bff511248; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1724519722,1724550692,1724573571,1724677634; HMACCOUNT=0F22F3009AFBFDB5; tst=r; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1724677649; SESSIONID=4x5hDqUtjX2T6v8PzXISLOdz9CRPAmFrkLKZX1TE5rV; JOID=W1kTA0mwcMibNNs5MrmjXWhGXmEljA_78Xuia0DRIP3JAKYJZa4PAPQ31Dg2BuU8mB_xPYekAG_zI_Oh75W0JAc=; osd=UFsVA067cs6bM9A7NLmkVmpAXmYujgn79nCgbUDWK__PAKECZ6gPB_810jgxDec6mBj6P4GkB2TxJfOm5JeyJAA=; BEC=36dafdc5edb6c00297b032c63dc4b447',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
        }
    })
    let decodedStr = iconv.decode(new Buffer.from(response.data), 'utf8');
    const $ = cheerio.load(decodedStr);
    const itemElement = $('.App-main .Topstory-content .Topstory-hot>.HotList-list>.HotItem');
    let ZHData = [];
    itemElement.each((i, item) => {
        const indexes = $(item).find('.HotItem-index>.HotItem-rank').text();
        const title = $(item).find('.HotItem-content>a>.HotItem-title').text();
        const content = $(item).find('.HotItem-content>a>.HotItem-excerpt').text();
        const heat = $($(item).find('.HotItem-content>.HotItem-metrics>svg')[0].nextSibling).text();
        const href = $(item).find('.HotItem-content>a').attr('href');
        ZHData.push({
            indexes,
            title,
            content,
            heat,
            href
        })

    })
    return ZHData;
}

const getBDTopSearch = async () => {
    const result = await axios.get("https://top.baidu.com/board?tab=realtime&sa=fyb_realtime_31065", {
        responseType: 'arraybuffer',
        headers: {
            Cookie: 'BIDUPSID=897A463CBC59C0FDF4154FA713C868B7; PSTM=1693463629; BAIDUID=897A463CBC59C0FDE0FC7C7FBF1CCB7A:FG=1; MCITY=-179%3A; H_WISE_SIDS_BFESS=40206_40211_40217_40224_40249_40273_40294_40291_40288_40286_40317; H_PS_PSSID=40211_40079_40364_40351_40381_40366_40312_40460_40481_40317_39662_40506_40513; delPer=0; BA_HECTOR=010585842h81802h8l24818h59m1841j025s61s; BAIDUID_BFESS=897A463CBC59C0FDE0FC7C7FBF1CCB7A:FG=1; ZFY=L:BuBCrOkaCHrHKD48Thl2n8Ck5crxGAQ7n78y26kvow:C; H_WISE_SIDS=40211_40079_40364_40351_40381_40366_40312_40460_40481_40317_39662_40506_40513; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; PSINO=3; BDUSS=XlzVUM5Mi1LZXdlY1ZJYkcwdnR2UHNXNkN3SWR1YjFsdWhVQ0thbEU2azdxU2htSUFBQUFBJCQAAAAAAAAAAAEAAAC0-wpP0e680rXEyMvC8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADscAWY7HAFmQ; BDUSS_BFESS=XlzVUM5Mi1LZXdlY1ZJYkcwdnR2UHNXNkN3SWR1YjFsdWhVQ0thbEU2azdxU2htSUFBQUFBJCQAAAAAAAAAAAEAAAC0-wpP0e680rXEyMvC8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADscAWY7HAFmQ; __bid_n=18e7456e87d9776c086b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YyZWRkZmY4OTY2ODhkMDYxMDAwYTI4Mzc1NjBjMDc4ZGM0YjYyYTQxNWRlYWNkMjFlZTVmNzQ2ZmE3ZjFhODk0ZmNiNWMzZDAzYmQ2YzkwN2Y3Mzg4ZmE5YjlmNWVhMzA2ZjI1NzQxZjM5MDZkZmM1NDZhOTVhM2JmMTZiZTlkZmU5OGM0MDk4NmI0MQ==; RT="z=1&dm=baidu.com&si=0705a684-1c1f-4917-b47e-c0976b47fb93&ss=lu6kf1j5&sl=f&tt=epx&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=16cf1&ul=16x6n&hd=16x6z',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
        }
    });
    const decodedContent = iconv.decode(Buffer.from(result.data), 'utf8');
    const $ = cheerio.load(decodedContent);
    const listElement = $('.rel').find('.category-wrap_iQLoo');
    const data = [];
    listElement.each((index, item) => {
        if (index !== 0) {
            const indexes = $(item).find('.img-wrapper_29V76>.index_1Ew5p').text().trim();
            const href = $(item).find('.content_1YWBm>a').attr('href').trim();
            const title = $(item).find('.content_1YWBm .c-single-text-ellipsis').text().trim();
            const content = $(item).find('.content_1YWBm .hot-desc_1m_jR').text().replace('查看更多>', '').trim();
            const heat = $(item).find('.trend_2RttY .hot-index_1Bl1a').text().trim();
            const imgSrc = $(item).find('.img-wrapper_29V76 img').attr('src');
            data.push({
                indexes,
                href,
                title,
                content,
                heat,
                imgSrc,
            })
        }
    })
    return data;
}

module.exports = {
    getWBTopSearch,
    getZHTopSearch,
    getBDTopSearch,
}